import {
  getGoodPointsTable,
  editGoodPointsRecord
} from '../../api/interactive'
import {
  GOOD_POINTS_LIST,
  GOOD_POINTS_CURRENT_ROW,
  GOOD_POINTS_DATA_COUNT,
  GOOD_POINTS_HISTORY_DATA,
  GOOD_POINTS_PAGE_SIZE
} from '../motations-type'

export default {
  state: {
    goodPointsList: [],
    goodPointsDataCount: null,
    goodPointsPageSize: 20,
    goodPointsHistoryData: [],
    row: {
      host: '',
      name: '',
      img: '',
      score: '',
      content: '',
      etime: '',
      ctime: '',
      state: '',
      type: '',
      des: '',
      price: '',
      salcnt: '',
      sort: '',
      top: '',
      topState: false
    }
  },
  getters: {
    goodPointsList: state => state.goodPointsList
  },
  mutations: {
    [GOOD_POINTS_LIST] (state, data) {
      state.goodPointsList = data
    },
    [GOOD_POINTS_CURRENT_ROW] (state, data) {
      state.row = data
    },
    [GOOD_POINTS_DATA_COUNT] (state, data) {
      state.goodPointsDataCount = data
    },
    [GOOD_POINTS_HISTORY_DATA] (state, data) {
      state.goodPointsHistoryData = data
    },
    [GOOD_POINTS_PAGE_SIZE] (state, data) {
      state.goodPointsPageSize = data
    }
  },
  actions: {
    async getGoodPointsList ({ commit, rootState }, playload) {
      let data = {}
      let newData = []
      if (Object.keys(playload).length > 0) {
        data = playload
      }
      const resp = await getGoodPointsTable(data)
      let host = resp.data.host
      resp.data.data.rows.map((item) => {
        newData = [...newData, {
          id: item.id,
          host: host,
          name: item.name,
          img: item.img,
          score: item.score.toString(),
          content: item.content,
          etime: item.etime,
          ctime: item.ctime,
          state: item.state,
          type: item.type.toString(),
          des: item.des,
          price: item.price.toString(),
          salcnt: item.salcnt,
          sort: item.sort,
          top: item.top,
          topState: item.top === 1
        }]
      })
      commit(GOOD_POINTS_LIST, newData)
      commit(GOOD_POINTS_DATA_COUNT, resp.data.data.total)
      if (newData.length < rootState.goodPoints.goodPointsPageSize) {
        commit(GOOD_POINTS_HISTORY_DATA, newData)
      } else {
        commit(GOOD_POINTS_HISTORY_DATA, newData.slice(0, rootState.goodPoints.goodPointsPageSize))
      }
      return resp
    },
    async editGoodPointsData ({ commit, rootState }, playload) {
      let data = {}
      if (Object.keys(playload).length > 0) {
        data = playload
      }
      const resp = await editGoodPointsRecord(data)
      return resp
    },
    async getGoodPointsCurrentRow ({ commit, rootState }, row) {
      commit(GOOD_POINTS_CURRENT_ROW, row)
    },
    async changeGoodPointsPage ({ commit, rootState }, index) {
      let data = rootState.goodPoints.goodPointsList
      let _start = (index - 1) * rootState.goodPoints.goodPointsPageSize
      let _end = index * rootState.goodPoints.goodPointsPageSize
      commit(GOOD_POINTS_HISTORY_DATA, data.slice(_start, _end))
    },
    async changeGoodPointsPageSize ({ commit, rootState }, value) {
      commit(GOOD_POINTS_PAGE_SIZE, value)
    }
  }
}
